package com.cy.controller;

import com.cy.service.ICartService;
import com.cy.utils.JsonResult;
import com.cy.vo.CartVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartController extends BaseController{
    @Autowired
    private ICartService iCartService;

    @RequestMapping("/add_to_cart")
    public JsonResult<Void>addToCart(Integer pid, Integer amount,
                                     HttpSession session){
        iCartService.addToCart(
                getuidFromSession(session),
                pid,
                amount,
                getUsernameFromSession(session));

        return new JsonResult<Void>(OK);

    }

    @RequestMapping({"","/"})
    public JsonResult<List<CartVO>>getVOByUid(HttpSession session){

        List<CartVO> data =iCartService.getVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/{cid}/num/add")
    public JsonResult<Integer>addNum(@PathVariable("cid") Integer cid, HttpSession session){
        Integer data = iCartService.addNum(cid, getuidFromSession(session), getUsernameFromSession(session));
        return  new JsonResult<>(OK,data);
    }

    @RequestMapping("/list")
    public JsonResult<List<CartVO>>getVOByCids(Integer []cids,HttpSession session){
        List<CartVO> data= iCartService.getVOByCid(getuidFromSession(session),cids);
        return new JsonResult<>(OK,data);


    }







}
